﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VatTuBUS1;
using VatTuDTO1;
using VatTuDTO;

namespace CungUngXayDung.admin
{
    public partial class ChiTietHoaDon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadHoaDon();
            LoadChiTietHoaDon();
        }

        public void LoadHoaDon()
        {
            int maHoaDon = int.Parse(Request.QueryString["id"].ToString());
            HoaDonDTO hoadon = new HoaDonBUS().SelectHoaDon(maHoaDon);
            int maNguoiDung = hoadon.MaNguoiDung;
            NguoiDungDTO nguoidung = new NguoiDungBUS().SelectNguoiDung(maNguoiDung);
            int maTinhTrang = hoadon.MaTinhTrang;
            TinhTrangDTO tinhTrang = new TinhTrangBUS().SelectTinhTrang(maTinhTrang);

            txtHinhThucThanhToan.Text = hoadon.HinhThucTHanhToan;
            txtTinhTrangHoaDon.Text = tinhTrang.TenTinhTrang;
            txtMaHoaDon.Text = hoadon.MaHoaDon.ToString();
            txtNguoiDung.Text = nguoidung.TenNguoiDung;
            txtMaNguoiDung.Text = nguoidung.MaNguoiDung.ToString();


            //load tinh trang hoa don vao combobox
            LoadDanhSachTinhTrangHoaDon();
        
        }
                public void LoadChiTietHoaDon()
            {
                      int maHoaDon = int.Parse(Request.QueryString["id"].ToString());
                List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();
                list = new ChiTietHoaDonBUS().LayDanhSachChiTietHoaDonTheoMa(maHoaDon);

                List<ChiTietHoaDonDTO1> list1 = new List<ChiTietHoaDonDTO1>();
                    ChiTietHoaDonDTO1 ct1;
                foreach (ChiTietHoaDonDTO ct in list)
                {
                    ct1=new ChiTietHoaDonDTO1();
                    ct1.MaHoaDon=ct.MaHoaDon;
                    ct1.MaChiTietHoaDon=ct.MaChiTietHoaDon;
                    ct1.SoLuong=ct.SoLuong;
                    ct1.DonGiaBan=ct.DonGiaBan;
                    SanPhamDTO sp=new SanPhamBUS().SelectSanPham(ct.MaSanPham);

                    ct1.TenSanPham=sp.TenSanPham;
                    list1.Add(ct1);
                }

                GridView1.DataSource = list1;
                GridView1.DataBind();
                Dispose();
            }

                //---------------------------------
                void LoadDanhSachTinhTrangHoaDon()
                {
                    List<TinhTrangDTO> lst = new List<TinhTrangDTO>();
                    lst = new TinhTrangBUS().SelectTinhTrang();
                    ddlTinhTrangHoaDon.DataSource = lst;
                    ddlTinhTrangHoaDon.DataValueField = "MaTinhTrang";
                    ddlTinhTrangHoaDon.DataTextField = "TenTinhTrang";
                    ddlTinhTrangHoaDon.DataBind();
                    Dispose();
                }


                protected void btnCancel_Click(object sender, EventArgs e)
                {
                    string url = "QLHoaDon.aspx";
                    Response.Redirect(url);
                }

                //su ly cap nhat lai san pham
                protected void btnSave_Click(object sender, EventArgs e)
                {
                    
                    string id = Request.QueryString["id"].ToString();
                    int dk = Convert.ToInt32(id);
                    if (dk != 0)
                    {

                        string hinhThucThanhToan = Request.Form[5].ToString();
                        int MaTinhTrang = int.Parse(Request.Form[7].ToString());


                        new HoaDonBUS().UpdateHoaDon(dk,MaTinhTrang);
                        new HoaDonBUS().UpdateHoaDonHinhThucThanhToan(dk,hinhThucThanhToan);
                        string url = "QLHoaDon.aspx";
                        Response.Redirect(url);
                    }
                    //if (dk == 0)
                    //{

                    //    lsp.TenLoaiSanPham = Request.Form[2].ToString();
                    //    lsp.MaChungLoai = int.Parse(Request.Form[3].ToString());

                    //    new LoaiSanPhamBUS().InsertLoaiSanPham(lsp);
                    //    string url = "QLLoaiSanPham.aspx";
                    //    Response.Redirect(url);
                    //}
                }
}
}